<?php
/**
 * 网春主持人和拜年对象选择
 */
require_once dirname(dirname(__DIR__)) . "/common.php";

use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\cls\tom\Config;
use app\model\Activity;

$redis = SysResource::getRedis();
$cacheKeyObject = REDIS_PREFIX . "bainian_object_" . rand(1, 3);
$cacheKeyHost = REDIS_PREFIX . "bainian_host_" . rand(1, 3);
$cacheTime = rand(20, 30);

try {

    $host = $redis->get($cacheKeyHost); // 主持人缓存数据
    if (!empty($host)) {
        $host = json_decode($host, true);
    }
    $object = $redis->get($cacheKeyObject); // 拜年对象缓存数据
    if (!empty($object)) {
        $object = json_decode($object, true);
    }

    if (!empty($host) && !empty($object)) {
        // 都有缓存直接返回
        Response::output(0, "查询成功", $host, $object);
    } else {
        $db = SysResource::getDB(false);
        $Activity = new Activity($db);
        if (empty($host)) {
            $host = $Activity->host();
            $redis->set($cacheKeyHost, json_encode($host), $cacheTime);
        }

        if (empty($object)) {
            $object = $Activity->select_person();
            $redis->set($cacheKeyObject, json_encode($object), $cacheTime);
        }
        Response::output(0, "查询成功", $host, $object);
    }

    // //查询数据库
    // $db = SysResource::getDB(false);
    // $Activity = new Activity($db);
    // //查询所有的主持人
    // $host = $Activity->host();
    // //查询所有拜年对象
    // $select_person = $Activity->select_person();
    // if (empty($host)) {
    //     $host = [];
    // }
    // if (empty($select_person)) {
    //     $select_person = [];
    // }
    // Response::output(0, "查询成功", $host, $select_person);

} catch (DBException $e) {
    Response::output(-1, "系统忙碌中");
}